package com.zsl.controller.Admin;

import com.alibaba.fastjson.JSON;
import com.zsl.service.CategoryService;
import com.zsl.service.impl.CategoryServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "DeleteCategory",urlPatterns = "/delete/category")
public class DeleteCategory extends HttpServlet {
    CategoryService categoryService=new CategoryServiceImpl();
    //TODO 测试，存在书籍绑定分类的情况下，删除该分类的情况
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        Map<String, Object> res = new HashMap<>();

        String c_id=request.getParameter("c_id").equals("")?null:request.getParameter("c_id");
        if(c_id==null){
            res.put("code",0);
            res.put("line",0);
            res.put("msg","参数错误");
            PrintWriter out = response.getWriter();
            out.write(JSON.toJSONString(res));
            return;
        }
        //TODO 删除
        try {
            res=categoryService.deleteCategory(c_id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            res.put("code",0);
            res.put("line",0);
            res.put("msg","内部错误");
        }finally {
            PrintWriter out = response.getWriter();
            out.write(JSON.toJSONString(res));
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
